package com.QK.cnstudy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

import com.QK.cnstudy.adapter.ResultAdapter;
import com.QK.cnstudy.bean.Quiz;
import com.QK.cnstudy.bean.Subject;
import com.QK.cnstudy.db.DBManager;
import com.QK.cnstudy.entity.Sentence;
import com.QK.cnstudy.entity.Work;
import com.QK.cnstudy.entity.WrongSubject;

public class ResultActivity extends Activity {

    public boolean hasAddWrongSubject = false;
    public Activity activity;
    private DBManager dbManager;
    private int totalTestNum;
    private int wrongTestNum;

    private List<Subject> wrongSubjectList = new ArrayList<Subject>();

    private Quiz quiz;

    private ListView resultListView;

    private ResultAdapter adapter;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    public final static int MSG_ADDWRONG = 0;

    final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_ADDWRONG:
                if (hasAddWrongSubject == true) {
                    Toast.makeText(activity, "已经添加过了哦", Toast.LENGTH_SHORT).show();
                    return;
                }

                for (int i = 0; i < wrongSubjectList.size(); i++) {
                    WrongSubject wrongSubject = new WrongSubject(wrongSubjectList.get(i));
                    wrongSubject.saveToDB(dbManager.getSqLiteDatabase());
                    Toast.makeText(activity, "添加成功", Toast.LENGTH_SHORT).show();
                }
                hasAddWrongSubject = true;
                break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        activity = this;
        dbManager = CnStudyApp.app.dbManager;
        getView();

        Bundle bundle = getIntent().getExtras();
        quiz = (Quiz) bundle.getSerializable("quiz");

        analysisQuiz();
        setListViewData();
        adapter = new ResultAdapter(this, list, mHandler);
        resultListView.setAdapter(adapter);
    }

    public void analysisQuiz() {
        totalTestNum = quiz.getTestSubjectNum();
        for (int i = 0; i < totalTestNum; i++) {
            Subject subject = quiz.getSubjects().get(i);
            if (subject.getAnswer() != subject.getSelectAnswer()) {
                wrongTestNum++;
                subject.index = i + 1;
                wrongSubjectList.add(subject);
            }
        }
    }

    public void setListViewData() {
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("flag", ResultAdapter.FLAG_TITLE);
        map1.put("key", "测试内容");
        Work work = dbManager.getWorkById(quiz.getTestWorkId());
        map1.put("value", work.getName());
        list.add(map1);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("flag", ResultAdapter.FLAG_TITLE);
        map2.put("key", "题目数");
        map2.put("value", quiz.getSubjectNum() + "");
        list.add(map2);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("flag", ResultAdapter.FLAG_TITLE);
        map3.put("key", "正确题数");
        map3.put("value", (quiz.getSubjectNum() - wrongTestNum) + "");
        list.add(map3);

        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("flag", ResultAdapter.FLAG_TITLE);
        map4.put("key", "正确率");
        map4.put("value", (quiz.getSubjectNum() - wrongTestNum) * 100 / quiz.getSubjectNum() + "%");
        list.add(map4);
        Map<String, Object> map5 = new HashMap<String, Object>();
        map5.put("flag", ResultAdapter.FLAG_TITLE);
        map5.put("key", "错题内容");
        map5.put("value", "");
        list.add(map5);
        for (int i = 0; i < wrongSubjectList.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("flag", ResultAdapter.FLAG_WRONG);
            Subject subject = wrongSubjectList.get(i);
            Sentence sentence = dbManager.getSentenceById(subject.getSentenceId());
            map.put("content", subject.index + ". " + sentence.getContent());
            map.put("translate", ((char) (subject.getAnswer() + 'A')) + ". " + sentence.getTranslation());
            list.add(map);
        }
        Map<String, Object> map6 = new HashMap<String, Object>();
        map6.put("flag", ResultAdapter.FLAG_NAVI);

        list.add(map6);
    }

    public void getView() {
        resultListView = (ListView) findViewById(R.id.result_list);
    }
}
